package emergence.game.hud;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import emergence.entities.Player;
import emergence.game.Game;
import emergence.game.gfx.ColoredSprite;

public class StatBlockViewer extends JPanel {

    private static final long serialVersionUID = 1L;

    private JLabel label;
    private BufferedImage bufferedIconImage;
    private ImageIcon iconImage;
    private JLabel icon;

    private JPanel header;
    private JPanel iconPanel;
    private JPanel iconWrapper;
    private JPanel propsWrapper;

    private Font titleFont = new Font("Arial", Font.BOLD, 16);
    private Font wordFont = new Font("Arial", Font.PLAIN, 12);

    public StatBlockViewer(Player player) {

        ArrayList<DispProp> props = new ArrayList<>();
        props.add(new DispProp("NAME", "Jip"));
        props.add(new DispProp("ATTACK", "42"));
        props.add(new DispProp("DEFENSE", "39"));

        this.setLayout(new GridBagLayout());

        header = new JPanel();
        header.setLayout(new BoxLayout(header, BoxLayout.LINE_AXIS));

        label = new JLabel("Viewer");
        label.setFont(titleFont);
        label.setHorizontalAlignment(JLabel.CENTER);

        header.add(Box.createHorizontalGlue());
        header.add(label);
        header.add(Box.createHorizontalGlue());

        header.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));

        ColoredSprite sprite = player.staticSprite();
        bufferedIconImage = new BufferedImage(sprite.width(), sprite.height(), BufferedImage.TYPE_INT_RGB);
        int imagePixels[] = ((DataBufferInt) bufferedIconImage.getRaster().getDataBuffer()).getData();

        for (int i = 0; i < sprite.pixels().length; i++) {
            for (int j = 0; j < sprite.pixels()[0].length; j++) {
                if (sprite.pixels()[i][j] < 255) {
                    imagePixels[i + (j * sprite.width())] = Game.colors[sprite.pixels()[i][j]];
                }
                else {
                    imagePixels[i + (j * sprite.width())] = this.getBackground().hashCode();
                }
            }
        }

        Image resizedImage = bufferedIconImage.getScaledInstance(sprite.width() * 3, sprite.height() * 3, Image.SCALE_SMOOTH);
        iconImage = new ImageIcon(resizedImage);
        icon = new JLabel(iconImage);

        iconPanel = new JPanel();
        iconPanel.setLayout(new BoxLayout(iconPanel, BoxLayout.LINE_AXIS));
        iconPanel.add(Box.createHorizontalGlue());
        iconWrapper = new JPanel();
        iconWrapper.add(icon);
        iconWrapper.setBorder(BorderFactory.createLoweredBevelBorder());
        iconPanel.add(iconWrapper);
        iconPanel.add(Box.createHorizontalGlue());
        header.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));

        propsWrapper = new JPanel();
        propsWrapper.setLayout(new BoxLayout(propsWrapper, BoxLayout.PAGE_AXIS));

        for (DispProp property : props) {
            String key = property.key();
            String value = property.value();
            JPanel comp = new JPanel();
            comp.setLayout(new BoxLayout(comp, BoxLayout.LINE_AXIS));

            JLabel compLabel = new JLabel(key + ": " + value);
            compLabel.setFont(wordFont);
            compLabel.setHorizontalAlignment(JLabel.LEFT);

            comp.add(Box.createHorizontalGlue());
            comp.add(compLabel);
            comp.add(Box.createHorizontalGlue());

            propsWrapper.add(Box.createRigidArea(new Dimension(0, 5)));
            propsWrapper.add(comp);
        }

        propsWrapper.add(Box.createRigidArea(new Dimension(0, 5)));

        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 0;
        c.fill = GridBagConstraints.NONE;
        c.weightx = 0.0;
        c.weighty = 0.0;
        c.anchor = GridBagConstraints.PAGE_START;
        this.add(header, c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 1;
        c.fill = GridBagConstraints.NONE;
        c.weightx = 0.0;
        c.weighty = 0.0;
        c.anchor = GridBagConstraints.PAGE_START;
        this.add(iconPanel, c);

        c = new GridBagConstraints();
        c.gridx = 0;
        c.gridy = 2;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 0.0;
        c.weighty = 1.0;
        c.anchor = GridBagConstraints.PAGE_START;
        this.add(propsWrapper, c);
    }

    public static class DispProp {

        private String key;
        private String value;

        public DispProp(String key, String value) {
            this.key = key;
            this.value = value;
        }

        public String key() {
            return key;
        }

        public String value() {
            return value;
        }
    }

}
